package freecomm.study.notes;

import android.content.Context;
import android.text.Editable;
import android.text.Spanned;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.EditText;

public class MyEditText extends EditText
{

    public static final int ADD_PHONE_TO_CONTACT = Menu.FIRST + 100;
    public static final int SEND_SMS_TO_PHONE = Menu.FIRST + 101;
    public static final int COPY_URL = Menu.FIRST + 102;
    public static final int ADD_EMAIL_TO_CONTACT = Menu.FIRST + 103;
    public static final int SEND_EMAIL_TO_ADDRESS = Menu.FIRST + 104;
    public static final int CALL_PHONE_NUMBER = Menu.FIRST + 105;

    public MyEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MyEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyEditText(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onCreateContextMenu(ContextMenu menu)
    {
        // TODO Auto-generated method stub
        Editable mText = getText();
        String mString = "";
        if (getText() instanceof Spanned)
        {
            int selStart = getSelectionStart();
            int selEnd = getSelectionEnd();

            int min = Math.min(selStart, selEnd);
            int max = Math.max(selStart, selEnd);

            URLSpan[] urls = ((Spanned) mText)
                    .getSpans(min, max, URLSpan.class);
            mString = mText.toString().substring(min, max);
            if (urls.length == 1)
            {
                urls[0].getSpanTypeId();
                if (mString.indexOf('@') < 0)
                {

                    menu.add(0, ADD_PHONE_TO_CONTACT, 0,
                            "添加" + mString + "到联系人")
                            .setOnMenuItemClickListener(mHanlder);
                    menu.add(0, SEND_SMS_TO_PHONE, 0, "给" + mString + "发短信")
                            .setOnMenuItemClickListener(mHanlder);
                    menu.add(0, COPY_URL, 0, "复制").setOnMenuItemClickListener(
                            mHanlder);
                    menu.add(0, CALL_PHONE_NUMBER, 0, "呼叫" + mString).setOnMenuItemClickListener(mHanlder);
                } else
                {
                    menu.add(0, ADD_EMAIL_TO_CONTACT, 0,
                            "添加" + mString + "到联系人")
                            .setOnMenuItemClickListener(mHanlder);
                    menu.add(0, SEND_EMAIL_TO_ADDRESS, 0, "给" + mString + "发邮件")
                            .setOnMenuItemClickListener(mHanlder);
                    menu.add(0, COPY_URL, 0, "复制").setOnMenuItemClickListener(
                            mHanlder);
                }
            }

            menu.setHeaderTitle(mString);
        }
    }

    public void setOnMenuItemClickListener(OnMenuItemClickListener mHanlder)
    {
        this.mHanlder = mHanlder;
    }

    private OnMenuItemClickListener mHanlder = new OnMenuItemClickListener()
    {

        @Override
        public boolean onMenuItemClick(MenuItem item)
        {
            // TODO Auto-generated method stub
            Log.e("MyError", "Haven't imported menu item click listener");
            return false;
        }
    };
}
